<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false" width="50%" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="名称" clearable/>
      </el-form-item>
      <el-form-item label="容量" prop="capacity">
        <el-input v-model="dataForm.capacity" :disabled="disabled" placeholder="容量" clearable type="number"/>
      </el-form-item>
      <el-form-item label="位置" prop="location">
        <el-input v-model="dataForm.location" :disabled="disabled" placeholder="位置" clearable/>
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-input v-model="dataForm.status" :disabled="disabled" placeholder="状态" clearable/>-->
<!--      </el-form-item>-->
      <el-form-item label="说明" prop="description">
        <el-input v-model="dataForm.description" :disabled="disabled" placeholder="说明" clearable/>
      </el-form-item>
      <el-form-item label="负责人" prop="master">
        <el-input v-model="dataForm.master" :disabled="disabled" placeholder="负责人" clearable/>
      </el-form-item>
<!--      <el-form-item label="默认仓库" prop="defaulted">-->
<!--        <el-switch v-model="dataForm.defaulted" :disabled="disabled" placeholder="默认仓库" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否"/>-->
<!--      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: '',
          name: '',
          location: '',
          capacity: '',
          status: '',
          description: '',
          master: '',
          defaulted: ''},
        dataRule: {
          name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
          capacity: [{required: true, message: '容量不能为空', trigger: 'blur'}],
          status: [{required: true, message: '状态不能为空', trigger: 'blur'}],
          other: []
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/psi/warehouse/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.info
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/psi/warehouse/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({ message: '操作成功', type: 'success', duration: 1500 })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
